package com.evan.designpatterns.proxy.dbroute.proxy;

import com.evan.designpatterns.proxy.dbroute.IOrderService;
import com.evan.designpatterns.proxy.dbroute.Order;
import com.evan.designpatterns.proxy.dbroute.db.DynamicDatasourceEntity;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author :liujiapeng01
 * @date :2019-03-14 18:34
 **/
public class StaticProxy implements IOrderService {

    private SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");


    private IOrderService iOrderService;

    public StaticProxy(IOrderService iOrderService) {
        this.iOrderService = iOrderService;
    }

    @Override
    public int insertOrder(Order order) {
        Long createTime = order.getCreateTime();
        String format = "DB"+yearFormat.format(new Date(createTime));
        DynamicDatasourceEntity.set(format);
        System.out.println("自动切换为"+format);
        int i = this.iOrderService.insertOrder(order);
        DynamicDatasourceEntity.remove();
        return i;
    }
}
